﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;
using System.Data;
using System.IO;
using System.Net;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminAddnews : System.Web.UI.Page
    {
        public Int32 NewsID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["NewsID"] != null)
                {
                    try
                    {
                        NewsID = Int32.Parse(Request.QueryString["NewsID"].ToString());
                    }
                    catch (Exception ex)
                    {
                        NewsID = -1;
                    }
                }

                if (NewsID == -1)
                {
                    ViewState["action"] = HangSo.ACTION_ADD;

                }
                else
                {
                    News thenews = NewsProvider.GetNewsbyID(NewsID);

                    if (thenews == null)
                        Response.Redirect("AdminNews.aspx");
                    else
                    {
                        this.txtTitleNews.Text = thenews.TitleNews;
                       FCKContent.Value = thenews.ContentNews;
                        this.txtTypeNews.Text = thenews.TypeNews;
                        ViewState["ID"] = thenews.NewsID;
                        ViewState["action"] = HangSo.ACTION_UPDATE;
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminNews.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            News theNews = new News();
            theNews.TitleNews = this.txtTitleNews.Text.Trim();
            theNews.ContentNews = FCKContent.Value.ToString().Trim();
            theNews.TypeNews = this.txtTypeNews.Text.Trim();
           
            if (action == HangSo.ACTION_ADD)
            {
                theNews.NewsID = -1;
                NewsProvider.IUNews(theNews, true);
            }
            else
            {
                theNews.NewsID = Int32.Parse(ViewState["ID"].ToString());
                NewsProvider.IUNews(theNews, false);
            }

            Response.Redirect("AdminNews.aspx");
        }

        protected void btnapply_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            News theNews = new News();
            theNews.TitleNews = this.txtTitleNews.Text.Trim();
            theNews.ContentNews = FCKContent.Value.ToString().Trim();
            theNews.TypeNews = this.txtTypeNews.Text.Trim();

            if (action == HangSo.ACTION_ADD)
            {
                theNews.NewsID = -1;
                NewsProvider.IUNews(theNews, true);
            }
            else
            {
                theNews.NewsID = Int32.Parse(ViewState["ID"].ToString());
                NewsProvider.IUNews(theNews, false);
            }
            ltrMessage.Text = "Påfør suksess";
        }
    }
}